package net.shopxx.action.admin;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.Admin;
import net.shopxx.entity.DeliveryCorp;
import net.shopxx.entity.DeliveryItem;
import net.shopxx.entity.DeliveryType;
import net.shopxx.entity.OrderItem;
import net.shopxx.entity.Payment;
import net.shopxx.entity.PaymentConfig;
import net.shopxx.entity.ProduceOrder;
import net.shopxx.entity.Refund;
import net.shopxx.entity.Reship;
import net.shopxx.entity.Shipping;
import net.shopxx.entity.Payment.PaymentType;
import net.shopxx.entity.Product.WeightUnit;
import net.shopxx.entity.Refund.RefundType;
import net.shopxx.service.AdminService;
import net.shopxx.service.DeliveryCorpService;
import net.shopxx.service.DeliveryTypeService;
import net.shopxx.service.PaymentConfigService;
import net.shopxx.service.ProduceOrderService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

/**
 * 后台Action类 - 生产分配
 */

@ParentPackage("admin")

public class ProduceOrderAction extends BaseAdminAction {

	private static final long serialVersionUID = 3567820378191246544L;
	private ProduceOrder produceOrder;
	private Payment payment;
	private Shipping shipping;
	private Refund refund;
	private Reship reship;

	private List<OrderItem> orderItemList;
	
	private List loadList;
	private List<DeliveryItem> deliveryItemList;
	@SuppressWarnings("unused")
	private List<Admin> produceOrderList;
	@Resource
	private DeliveryTypeService deliveryTypeService;
	@Resource
	private DeliveryCorpService deliveryCorpService;
	@Resource
	private PaymentConfigService paymentConfigService;

	@Resource
	private ProduceOrderService produceOrderService;	
	
	@Resource
	private AdminService adminService;	

   //生产单列表
	// 列表 
	public String list() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
			
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return LIST;
	}


	// 分配
	public String distrib() {	
		setResponseNoCache();
		return "distrib";
	}
	
	
	// 分配详情     
	public String info() {	
		setResponseNoCache();
		 
		ProduceOrder persistent = produceOrderService.load(id);
		
		 
		
		persistent.setDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setDistribStaff(produceOrder.getDistribStaff());
		
		persistent.setDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		
		persistent.setIsState("3");
		produceOrderService.update(persistent);
	
		redirectionUrl = "produce_order!list.action";
		return SUCCESS;
	}
	
	// 删除
	public String delete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				ProduceOrder persistent = produceOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				produceOrderService.update(persistent);
			}
			
		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("删除失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("删除成功！");
		
	}
	
	// 退单
	public String back() {
		try {

				ProduceOrder persistent = produceOrderService.load(id);
				persistent.setIsState("5");
				produceOrderService.update(persistent);

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("退单失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("退单成功！");
	}

	// 完成
	public String complete() {
		try {

				ProduceOrder persistent = produceOrderService.load(id);
				persistent.setIsState("4");
				produceOrderService.update(persistent);
				
				
				
				

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("退单失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("退单成功！");
	}
	
	
	//未分配列表
	public String wlist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "2"));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return "list1";
	}
	
	// 删除
	public String wdelete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				ProduceOrder persistent = produceOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				produceOrderService.update(persistent);
			}

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("删除失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("删除成功！");
		
	}
	
	
	// 分配
	public String wdistrib() {	
		setResponseNoCache();
		return "distrib1";
	}
	
	
	// 分配详情     
	public String winfo() {	
		setResponseNoCache();
		 
		ProduceOrder persistent = produceOrderService.load(id);
		
		 
		
		persistent.setDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setDistribStaff(produceOrder.getDistribStaff());
		
		persistent.setDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		
		persistent.setIsState("3");
		produceOrderService.update(persistent);
	
		redirectionUrl = "produce_order!wlist.action";
		return SUCCESS;
	}
	
	
	
	
	//已分配列表
	public String ylist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "3"));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return "list2";
	}
	
	// 删除
	public String ydelete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				ProduceOrder persistent = produceOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				produceOrderService.update(persistent);
			}

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("删除失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("删除成功！");
		
	}
	
	
	// 分配
	public String ydistrib() {	
		setResponseNoCache();
		return "distrib2";
	}
	
	
	// 分配详情     
	public String yinfo() {	
		setResponseNoCache();
		 
		ProduceOrder persistent = produceOrderService.load(id);
		
		 
		
		persistent.setDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setDistribStaff(produceOrder.getDistribStaff());
		
		persistent.setDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		

		produceOrderService.update(persistent);
	
		redirectionUrl = "produce_order!ylist.action";
		return SUCCESS;
	}
	
	
	//已完成列表
	public String oklist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "4"));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return "list3";
	}
	
	// 删除
	public String okdelete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				ProduceOrder persistent = produceOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				produceOrderService.update(persistent);
			}

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("删除失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("删除成功！");
		
	}

	
	
	//退单列表
	public String tlist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "5"));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return "list4";
	}
	
	// 删除
	public String tdelete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				ProduceOrder persistent = produceOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				produceOrderService.update(persistent);
			}

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("删除失败，生产单数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("删除成功！");
		
	}
	
	//废单列表
	public String flist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", false));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);
		return "list5";
	}
	
	//恢复生产单
	public String recovery() {
			ProduceOrder persistent = produceOrderService.load(id);
			persistent.setIsEnabled(true);
			produceOrderService.update(persistent);
	        redirectionUrl = "produce_order!flist.action";
	        return SUCCESS;
	}
	
	//工作负荷列表
	public String workLoad() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProduceOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", false));	
		pager = produceOrderService.findByPager(pager,detachedCriteria);			
	    loadList = produceOrderService.getLoadByDistrib();		
		return "load";
	}
	

	
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	
	// 获取所有重量单位
	public List<WeightUnit> getAllWeightUnit() {
		List<WeightUnit> allWeightUnit = new ArrayList<WeightUnit>();
		for (WeightUnit weightUnit : WeightUnit.values()) {
			allWeightUnit.add(weightUnit);
		}
		return allWeightUnit;
	}
	
	// 获取所有配送方式
	public List<DeliveryType> getAllDeliveryType() {
		return deliveryTypeService.getAll();
	}
	
	// 获取所有支付方式
	public List<PaymentConfig> getAllPaymentConfig() {
		return paymentConfigService.getAll();
	}
	
	// 获取支付类型（不包含在线充值）
	public List<PaymentType> getNonRechargePaymentTypeList() {
		List<PaymentType> paymentTypeList = new ArrayList<PaymentType>();
		for (PaymentType paymentType : PaymentType.values()) {
			if (paymentType != PaymentType.recharge) {
				paymentTypeList.add(paymentType);
			}
		}
		return paymentTypeList;
	}
	
	// 获取退款类型
	public List<RefundType> getRefundTypeList() {
		List<RefundType> refundTypeList = new ArrayList<RefundType>();
		for (RefundType refundType : RefundType.values()) {
			refundTypeList.add(refundType);
		}
		return refundTypeList;
	}
	
	// 获取所有物流公司
	public List<DeliveryCorp> getAllDeliveryCorp() {
		return deliveryCorpService.getAll();
	}




	public ProduceOrderService getProduceOrderService() {
		return produceOrderService;
	}


	public void setProduceOrderService(ProduceOrderService produceOrderService) {
		this.produceOrderService = produceOrderService;
	}


	public Payment getPayment() {
		return payment;
	}

	public void setPayment(Payment payment) {
		this.payment = payment;
	}

	public Shipping getShipping() {
		return shipping;
	}

	public void setShipping(Shipping shipping) {
		this.shipping = shipping;
	}

	public Refund getRefund() {
		return refund;
	}

	public void setRefund(Refund refund) {
		this.refund = refund;
	}

	public Reship getReship() {
		return reship;
	}

	public void setReship(Reship reship) {
		this.reship = reship;
	}

	public List<OrderItem> getOrderItemList() {
		return orderItemList;
	}

	public void setOrderItemList(List<OrderItem> orderItemList) {
		this.orderItemList = orderItemList;
	}

	public List<DeliveryItem> getDeliveryItemList() {
		return deliveryItemList;
	}

	public void setDeliveryItemList(List<DeliveryItem> deliveryItemList) {
		this.deliveryItemList = deliveryItemList;
	}


	public ProduceOrder getProduceOrder() {
		return produceOrder;
	}


	public void setProduceOrder(ProduceOrder produceOrder) {
		this.produceOrder = produceOrder;
	}


	public List<Admin> getProduceOrderList() {
		return adminService.getAll();
	}

	public void setProduceOrderList(List<Admin> produceOrderList) {
		this.produceOrderList = produceOrderList;
	}


	public List getLoadList() {
		return loadList;
	}


	public void setLoadList(List loadList) {
		this.loadList = loadList;
	}

}